<?php
class Admin_Model_Uzytkownik extends Zend_Db_Table_Abstract
{
    protected $_name = 'uzytkownik';
      
    public function dodajUzytkownika($imie,$nazwisko,$login,$haslo,$mail,$gg,$telefon,$adres)
    {
        $this->getAdapter()->beginTransaction();
        try{
        $dane=array(
          'imie'=>$imie,
          'nazwisko'=>$nazwisko,
          'login'=>$login,
          'haslo_hash'=>$this->hashuj($haslo),
          'email'=>$mail,
          'gg'=>$gg,
          'telefon'=>$telefon,
          'adres'=>$adres,
          'id_rola'=>2
          );
        
        $this->insert($dane);
        
        $this->getAdapter()->commit();
        return 1;
        }catch(Exception $e){
            return $e->getMessage();
           $this->getAdapter()->rollback();
        }
        
    }
    
    public function hashuj($haslo)
    {
        return md5($haslo);
    }
    
    public function sprawdzlogin($rekord)
    {
        $select = $this->getAdapter()->select()
                ->from(array('u' => 'uzytkownik'))
                ->where('u.login =?',$rekord)
                ->limit(1);
        $return =$this->getAdapter()->fetchAll($select);
        return count($return);
    }
    
     public function sprawdzmail($rekord)
    {
        $select = $this->getAdapter()->select()
                ->from(array('u' => 'uzytkownik'))
                ->where('u.email =?',$rekord)
                ->limit(1);
        $return =$this->getAdapter()->fetchAll($select);
        return count($return);
    }
    
    public function daj_liste()
    {
        $select=$this->getAdapter()->select()->from(array('u' => 'uzytkownik'))
                ->join(array('r' => 'rola'),'u.id_rola=r.id',array('nazwa'=>'nazwa','id_roli'=>'id'));
        return $this->getAdapter()->fetchAll($select);
    }
    
    public function daj_liste_banned()
    {
        $select=$this->getAdapter()->select()->from(array('u' => 'uzytkownik'))
                ->join(array('r' => 'rola'),'u.id_rola=r.id',array('nazwa'=>'nazwa','id_roli'=>'id'))
                ->where('u.zbanowany =?', 1);
        return $this->getAdapter()->fetchAll($select);
    }
    
    public function daj_liste_blocked()
    {
        $select=$this->getAdapter()->select()->from(array('u' => 'uzytkownik'))
                ->join(array('r' => 'rola'),'u.id_rola=r.id',array('nazwa'=>'nazwa','id_roli'=>'id'))
                ->where('u.aktywny =?', 0);
        return $this->getAdapter()->fetchAll($select);
    }
    
    public function banuj($login)
    {
        $data=array('aktywny'=>"0",'zbanowany'=>"1");
            $this->update($data,array('login =?' => $login));
    }
    
    public function odbanuj($login)
    {
        $data=array('aktywny'=>"1",'zbanowany'=>"0");
            $this->update($data,array('login =?' => $login));
    }
    
    
    
    public function zmienhaslo($stare, $nowe)
    {
        $this->getAdapter()->beginTransaction();
        try{
        $select = $this->getAdapter()->select()
                ->from(array('u' => 'uzytkownicy'))
                ->where('u.login =?',Zend_Auth::getInstance()->getStorage()->read()->dane['login']);
        $return =$this->getAdapter()->fetchAll($select);
        $haslos= $return[0][haslo];
        
        if($haslos==$stare)
        {
            $nowe_hash=$this->hashuj($nowe);
            $data=array('haslo'=>$nowe,'hash_haslo'=>$nowe_hash);
            $this->update($data,array('login =?' => Zend_Auth::getInstance()->getStorage()->read()->dane['login']));
            
        }
        $this->getAdapter()->commit();
        return 1;
        }catch(Exception $e){
             return $e->getMessage();
           $this->getAdapter()->rollback();
            
        }
        
    }
    
    public function zmienemail($haslo, $nowe)
    {
        $this->getAdapter()->beginTransaction();
        try{
        $select = $this->getAdapter()->select()
                ->from(array('u' => 'uzytkownicy'))
                ->where('u.login =?',Zend_Auth::getInstance()->getStorage()->read()->dane['login']);
        $return =$this->getAdapter()->fetchRow($select);
        $haslos= $return[haslo];
        
        if($haslos==$haslo)
        {
            
            $data=array('email'=>$nowe);
            $this->update($data,array('login =?' => Zend_Auth::getInstance()->getStorage()->read()->dane['login']));
            
        }
        $this->getAdapter()->commit();
        return 1;
        }catch(Exception $e){
             return $e->getMessage();
           $this->getAdapter()->rollback();
            
        }
        
    }
    public function dajNumer($login)
    {
        $select=$this->getAdapter()->select()
                ->from(array('u'=>'uzytkownicy'))
                ->where('u.login =?',$login);
        $efekt=$this->getAdapter()->fetchAll($select);
        return $efekt[0][ID];
    }
    
    public function czyAktywny($login)
    {
        $select=$this->getAdapter()->select()
                ->from(array('u'=>'uzytkownicy'))
                ->where('u.login =?',$login);
        $efekt=$this->getAdapter()->fetchRow($select);
        return $efekt['aktywny'];
    }
    
    public function aktywuj($login)
    {
        $data=array('aktywny'=>"1");
            $this->update($data,array('login =?' => $login));
    }
    
     
    
    public function dajID($login)
    {
        $select=$this->getAdapter()->select()
                ->from(array('u'=>'uzytkownicy'))
                ->where('u.login =?',$login);
        $efekt=$this->getAdapter()->fetchRow($select);
        return $efekt['ID'];
    }
    
    public function zmienRole($login,$id_rola)
    {
       $data=array('ID_rola'=>$id_rola);
            $this->update($data,array('login =?' => $login));
    }
}
?>
